package dev.polls.skycube;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Looper;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;



public class Splashscreen extends Activity{
	// wait time
private int _splashTime = 4000; 
	
	private Thread splashTread;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    // splash screen view
	    setContentView(R.layout.splash);
	    
	    LinearLayout lin=new LinearLayout(this);
	         
        ImageView Image =(ImageView) findViewById(R.id.load);
        Animation Jump = AnimationUtils.loadAnimation(this,R.anim.anim);
        Image.startAnimation(Jump);
        lin.setOrientation(android.widget.LinearLayout.VERTICAL);

	    
	    ConnectivityManager connectivitymanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
         final boolean connected = networkinfo != null && networkinfo.isAvailable()
                && networkinfo.isConnected();

	      
	    // thread for displaying the SplashScreen
	    splashTread = new Thread() {
	        @Override
	        public void run() {
	            try {	            	
	            	synchronized(this){
	            		wait(_splashTime);
	            	}
	            	
	            } catch(InterruptedException e) {} 
	            finally {
	                finish();
	                Looper.prepare();
	                if (connected == false) {
	                    Splashscreen.this.runOnUiThread(new Runnable() {

	                        public void run() {
	                            Toast.makeText(Splashscreen.this,
	                                    "No Internet Connection.", 3000).show();
	                        }
	                    });
	                    finish();
	                    
	                	} else {
	                		finish();
	                		startActivity(new Intent(Splashscreen.this,
                        		SkyCubeActivity.class));
	                	}
                    	Looper.loop();
	            }
	      }
	  };
	    
	    splashTread.start();
	}
	// Touch events  als je het wilt skippen voor testing
	@Override
	public boolean onTouchEvent(MotionEvent event) {
	    if (event.getAction() == MotionEvent.ACTION_DOWN) {
	    	synchronized(splashTread){
	    		splashTread.notifyAll();
	    	}
	    }
	    return true;
	}
	
}

